package com.bxd.baodan.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.LoginActivity;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.item.UserItem;
import com.bxd.baodan.util.DialogUtil;
import com.bxd.baodan.util.ETUtil;
import com.bxd.baodan.util.ViewUtil;

import java.lang.reflect.Field;

/**
 * fragment基类
 *
 * @author oldfeel
 *         <p>
 *         Created on: 2014-1-20
 */
public class BaseFragment extends Fragment {
    protected Activity context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        setHasOptionsMenu(true);
    }

    protected void openActivity(Class<?> className) {
        Intent intent = new Intent(App.getContext(), className);
        startActivity(intent);
        if (getActivity() != null) {
            getActivity().overridePendingTransition(R.anim.slide_in_right,
                    R.anim.slide_out_left);
        }
    }

    public void openActivity(Class<?> targetClass, String[] keys, String[] values) {
        Intent intent = new Intent(App.getContext(), targetClass);
        for (int i = 0; i < keys.length; i++) {
            intent.putExtra(keys[i], values[i]);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_in_right,
                R.anim.slide_out_left);
    }

    protected void openActivity(Class<?> className, Bundle b) {
        Intent intent = new Intent(App.getContext(), className);
        intent.putExtras(b);
        startActivity(intent);
        if (getActivity() != null) {

            getActivity().overridePendingTransition(R.anim.slide_in_right,
                    R.anim.slide_out_left);
        }
    }

    public void showToast(String string) {
        DialogUtil.getInstance().showToast(App.getContext(), string);
    }

    public void showSimpleDialog(String string) {
        DialogUtil.getInstance().showToast(App.getContext(), string);
    }

    public String getString(EditText et) {
        return ETUtil.getString(et);
    }

    public GridView getGridView(View view, int id) {
        return ViewUtil.getGridView(view, id);
    }

    public ListView findListView(View view, int id) {
        return ViewUtil.getListView(view, id);
    }

    public TextView getTextView(View view, int id) {
        return ViewUtil.getTextView(view, id);
    }

    public ImageView getImageView(View view, int id) {
        return ViewUtil.getImageView(view, id);
    }

    public ListView getListView(View view, int id) {
        return ViewUtil.getListView(view, id);
    }

    public Button getButton(View view, int id) {
        return ViewUtil.getButton(view, id);
    }

    public EditText getEditText(View view, int id) {
        return ViewUtil.getEditText(view, id);
    }

    public ImageButton getImageButton(View view, int id) {
        return ViewUtil.getImageButton(view, id);
    }

    public String getUserid() {
        return getUser().getU_id();
    }

    public UserItem getUser() {
        return LoginInfo.getInstance(App.getContext()).getUser();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
//		getActivity().overridePendingTransition(R.anim.slide_in_right,
//				R.anim.slide_out_left);
    }


    private boolean isSelected = false;


    public void onSelected() {
        isSelected = true;
    }

    public void unSelected() {
        isSelected = false;
    }

    /**
     * 通过反射的方式获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean loginStatus() {

        boolean isLogin = (LoginInfo.getInstance(App.getContext())
                .getUser().getU_id() != null);

        if (!isLogin) {
            Intent it = new Intent(getActivity(),
                    LoginActivity.class);
            startActivity(it);
        }

        return isLogin;
    }

}
